/*
  This is part of emma
  Atmega32 Timer/Counter 2 control routines.
 */
#include <avr/io.h>

// prescaler options
#define timc2_stop           0
#define timc2_no_prescaler   1
#define timc2_prescaler8     2
#define timc2_prescaler32    3
#define timc2_prescaler64    4
#define timc2_prescaler128   5
#define timc2_prescaler256   6
#define timc2_prescaler1024  7

#define TC2_OV_ENABLE() TC2_ov_interrupt(1)
#define TC2_OV_DISABLE() TC2_ov_interrupt(0)

void TC2_conf(uint8_t prescal)
{
  TCNT2 = 0; // Timer/Counter Register
  TCCR2 &= ~(1<<CS22)|~(1<<CS21)|~(1<<CS20); // clear previous prescaler settings
  TCCR2 |= prescal;			//configures prescaler to passed value
}

void TC2_ov_interrupt(int ON)
{
  if(ON)
    TIMSK |= 1<<TOIE2;   //enables overflow interrupt
  else
    TIMSK &= ~(1<<TOIE2);    //disables overflow interrupt
}

/*
sei();  //should go on main to enable global interrupts

Interrupt vector that should go on main
ISR( TIMER2_OVF_vect )
{
}
*/
